package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.pojo.MailBean;
import com.example.service.IUserService;
import com.example.utils.MailUtils;
import com.example.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;


/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author czx
 * @since 2022-04-28
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    MailUtils mailUtils;

    @Resource
    TemplateEngine templateEngine;

    /**
     * 发送手机号验证码
     *
     * @param user 用户
     * @return 验证码
     */
    @Override
    public int sendMsg(User user) {
        // 获取手机号
        String phone = user.getPhone();
        int code = 0;
        if (StringUtils.isNotEmpty(phone)) {
            // 获取随机的四位验证码
            code = ValidateCodeUtils.generateValidateCode(4);
            log.info("验证码：code = {}", code);
            // 由于短信服务需要付费，所以跳过短信发送
        }
        return code;
    }

    /**
     * 用户登录注册
     *
     * @param phone    手机号
     * @param email    邮箱
     * @param password 密码
     * @return 用户
     */
    @Override
    public User login(String phone, String email, String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(phone),User::getPhone, phone);
        if (password != null) {
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            wrapper.eq(User::getPassword, password);
            return this.getOne(wrapper);
        }
        User user = this.getOne(wrapper);
        if (user == null) {
            user = new User();
            if(StringUtils.isNotEmpty(email)){
                user.setEmail(email);
                user.setPhone(phone);
            }else{
                user.setPhone(phone);
            }
            // 默认密码为123456
            String pwd = "123456";
            pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
            user.setPassword(pwd);
            this.save(user);
        }
        return user;
    }

    /**
     * 根据传过来的user条件查询
     *
     * @param user user条件
     * @return 查询结果
     */
    @Override
    public User getByIdOrPhone(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        String phone = user.getPhone();
        Long id = user.getId();
        wrapper.eq(StringUtils.isNotEmpty(phone), User::getPhone, phone);
        wrapper.eq(id != null, User::getId, id);
        User one = this.getOne(wrapper);
        one.setPassword(null);
        return one;
    }

    /**
     * 更新用户信息
     *
     * @param user 用户
     * @return 更新结果
     */
    @Override
    public boolean updateUserInfo(User user) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(User::getName, user.getName());
        wrapper.set(User::getSex, user.getSex());
        if (StringUtils.isNotEmpty(user.getPassword())) {
            String password = user.getPassword();
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            wrapper.set(User::getPassword, password);
        }
        return this.update(wrapper);
    }

    /**
     * 发送邮件验证码
     *
     * @param email 用户邮箱
     * @return 验证码
     */
    @Override
    public String sendMail(String email) {
        String code = null;
        if (StringUtils.isNotEmpty(email)) {
            code = ValidateCodeUtils.generateValidateCodeString(4);
            Context context = new Context();
            // html填充动态属性值
            context.setVariable("code",code);
            String content = templateEngine.process("email",context);
            MailBean mailBean = new MailBean(email, "瑞吉外卖注册邮件", content);
            mailUtils.sendHtml(mailBean);
        }
        return code;
    }


}
